package com.yao.seckill.web.controller;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.GetResponse;
import com.yao.seckill.core.SeckillResponse;
import com.yao.seckill.web.pojo.Product;
import com.yao.seckill.web.vo.EmployeeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/10/1 15:57
 * FileName: EsController
 * Description: 1
 */

@RestController
@Slf4j
public class EsController {

    @Autowired
    private ElasticsearchClient client;

    @RequestMapping("es")
    SeckillResponse testIndex(Integer id) throws IOException {
        GetResponse<EmployeeVo> response = client.get(g -> g
                        .index("employee")
                        .id(String.valueOf(id)),
                EmployeeVo.class
        );

        if (response.found()) {
            EmployeeVo employee = response.source();
            log.error("employee=" + employee);
        } else {
            log.error ("employee not found");
        }
        return SeckillResponse.success(response.source());
    }

}
